﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HYI.SDBSERVER;
using Sunny.UI;

namespace SHRSYSTEM
{
    public partial class FMUSERBOX : Form
    {
        public string suserid;
        public string susername;
        public DBSERVER myADO;

        public FMUSERBOX()
        {
            InitializeComponent();
        }

        private void SetNodeCheckStatus(TreeNode tn, bool Checked)
        {
            if (tn == null) return;
            // Check children nodes
            foreach (TreeNode tnChild in tn.Nodes)
            {
                tnChild.Checked = Checked;
                SetNodeCheckStatus(tnChild, Checked);
            }
            // Set parent check status
            TreeNode tnParent = tn;
            int nNodeCount = 0;
            while (tnParent.Parent != null)
            {
                tnParent = (TreeNode)(tnParent.Parent);
                nNodeCount = 0;
                foreach (TreeNode tnTemp in tnParent.Nodes)
                    //if (tnTemp.Checked == Checked)
                    if (tnTemp.Checked)
                        nNodeCount++;

                //if (nNodeCount == tnParent.Nodes.Count)
                //  tnParent.Checked = Checked;
                if (nNodeCount>0)
                    tnParent.Checked = true;
                else
                    tnParent.Checked = false;
            }
        }

        private void doLoadMenu()
        {
            TreeNode tmp1; TreeNode tmp2; TreeNode tmp3;

            string sql = " select *"
              +" from("
              + "  select b.fpoint_no fno, b.fpoint_name fname, '0' fparent,0 fflag"
              + "  from i_ts_box a, a_ts_point b"
              + "  where a.fpoint_no = b.fpoint_no"
              + "  group by b.fpoint_no, b.fpoint_name"
              + "  union"
              + "  select a.fequipmentbarcode fno, fequipmenttag fname, a.fpoint_no fparent,ISNULL(b.user_id,0) fflag"
              + "  from i_ts_box a"
              + "  left join i_use_box b on b.user_id="+suserid+" and a.fequipmentbarcode = b.fequipmentbarcode"
              + "  where 1 = 1"
              + "  union "
              + "  select c.fequipmentbarcode fno,c.fequipmenttag fname,a.fequipmentbarcode fparent,ISNULL(b.user_id,0) fflag"
              + "  from i_ts_controlbox a"
              + "  left join i_ts_cabinet c on a.fcatbarcode = c.fequipmentbarcode"
              + "  left join i_use_box b on b.user_id=" + suserid + " and c.fequipmentbarcode = b.fequipmentbarcode"
              + "   where 1 = 1"
              +" ) s"
              + "  where 1 = 1"
              + "  order by fparent ";
            DataTable myHead = myADO.query(sql);

            if ((myHead is null) || (myHead.Rows.Count == 0))
            {
                return;
            }

            //1.第一级菜单
            DataRow[] drHead = myHead.Select("fparent ='0'");
            for (int i = 0; i < drHead.Length; i++)
            {
                tmp1 = new TreeNode(drHead[i]["fname"].ToString());
                tmp1.Tag = drHead[i]["fno"].ToString();
                tmp1.Checked = false;
                if (!drHead[i]["fflag"].ToString().Equals("0"))
                {
                    tmp1.Checked = true;
                }
                uiTreeView1.Nodes.Add(tmp1);

                //2.第二级菜单
                DataRow[] drItem = myHead.Select("fparent ='" + drHead[i]["fno"].ToString() + "'");
                for (int j = 0; j < drItem.Length; j++)
                {
                    tmp2 = new TreeNode(drItem[j]["fname"].ToString());
                    tmp2.Tag = drItem[j]["fno"].ToString();
                    if (!drItem[j]["fflag"].ToString().Equals("0"))
                    {
                        tmp2.Checked = true;
                    }
                    tmp1.Nodes.Add(tmp2);

                    //3.第三级菜单
                    DataRow[] drItem2 = myHead.Select("fparent ='" + drItem[j]["fno"].ToString() + "'");
                    for (int k = 0; k < drItem2.Length; k++)
                    {
                        tmp3 = new TreeNode(drItem2[k]["fname"].ToString());
                        tmp3.Tag = drItem2[k]["fno"].ToString();
                        if (!drItem2[k]["fflag"].ToString().Equals("0"))
                        {
                            tmp3.Checked = true;
                        }
                        tmp2.Nodes.Add(tmp3);
                    }
                }

            }
            uiTreeView1.ExpandAll();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FMUSERBOX_Load(object sender, EventArgs e)
        {
            pictureBox1.Left = this.Width - 32 - 2;
            pictureBox1.Top = 2;
            pictureBox1.Height = 32;
            pictureBox1.Width = 32;

            uiTitlePanel1.Text = susername;

            doLoadMenu();
        }

        private void uiTreeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                //Event call by mouse or key-press
                SetNodeCheckStatus(e.Node, e.Node.Checked);
            }
        }

        //遍历TreeView所有节点
        public void PrintTreeViewNode(TreeNodeCollection node)
        {
            foreach (TreeNode n in node)
            {
                Console.WriteLine(n.Text.ToString());
                if (n.Checked)
                {
                    string s = n.Tag.ToString();
                    string sql = "insert into i_use_box(user_id,fequipmentbarcode)"
                    + " values(" + suserid + ",'" + s + "')";
                    myADO.SQLList.Add(sql);
                    //Console.WriteLine("当前选择:" + s);
                }

                PrintTreeViewNode(n.Nodes);
            }
        }

        private void uiPanel1_Click(object sender, EventArgs e)
        {
            if (this.ShowAskDialog("确定保存吗?"))
            {
                myADO.SQLList.Clear();
                string sql = "delete from i_use_box where user_id=" + suserid; ;
                myADO.SQLList.Add(sql);

                PrintTreeViewNode(uiTreeView1.Nodes);

                bool IsOk;
                string sError = string.Empty;
                myADO.doSave(out IsOk, out sError);
                if (IsOk)
                {
                    DBCache myCache = new DBCache();
                    myCache.doCache();

                    UIMessageTip.ShowOk("保存成功");
                    this.Close();
                }
            }
        }
    }
}
